// 原型链

// obj -> Object.prototype -> null
// func -> Function.prototype -> Object.prototype -> null
// arr -> Array.prototype -> Object.prototype -> null

// 如果A沿着原型链能找到B.prototype，那么A instanceof B 为true
const arr: Array<number> = []
console.log(arr instanceof Object) // true
const func: Function = () => {}
console.log(func instanceof Object) // true


const instanceOf = (A: any, B: any): boolean => {
  let p = A
  while (p) {
    if (p === B.prototype) {
      return true
    }
    p = p.__proto__
  }
  return false
}

console.log(instanceOf([], Array)) // true
console.log(instanceOf([], Object)) // true
console.log(instanceOf([], Function)) // false
